package cn.edu.ctbu.dp01.controller;

import cn.edu.ctbu.dp01.entity.Student;
import cn.edu.ctbu.dp01.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/student")
public class StudentController {

    @Autowired
    private StudentService studentService;

    //更新学生数据
    @PostMapping("/update")
    public Student updateStudent(Student student) {
        return studentService.update(student);
    }

    @GetMapping("/list")
    public String getList(Model model) {
        List<Student> students = studentService.getAll();
        model.addAttribute("students", students);
        return "/student/list";
    }

    @PostMapping("/add")
    public Student add( Student student){
        studentService.add(student);
        return student;
    }
}
